package com.hzf.order.rpc.service.impl;

import com.hzf.common.annotation.BaseService;
import com.hzf.order.dao.mapper.*;
import com.hzf.order.dao.model.*;
import com.hzf.order.rpc.api.OrderApiService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * describe: TODO
 * creat_user: 白开水
 * creat_time: 2017/12/4   14:41
 **/
@Service
@Transactional
public class OrderApiServiceImpl implements OrderApiService {
    private static Logger _log = LoggerFactory.getLogger(OrderCartServiceImpl.class);

    @Autowired
    OrderCartMapper orderCartMapper;
    @Autowired
    OrderUserMapper orderUserMapper;
    @Autowired
    OrderUserInfoMapper orderUserInfoMapper;
    @Autowired
    OrderUserAddressMapper orderUserAddressMapper;
    @Autowired
    OrderNotityMapper orderNotityMapper;
    @Autowired
    OrderUserDemandMapper orderUserDemandMapper;
    @Override
    public List<OrderCart> selectByGoods(OrderCartExample example) {
        return orderCartMapper.selectByGoodsPrimaryKey(example);
    }

    @Override
    public int insertOrder(OrderCart orderCart) {
        OrderCartExample orderCartExample = new OrderCartExample();
        orderCartExample.createCriteria().andGoodsIdEqualTo(orderCart.getGoodsId()).andModelIdEqualTo(orderCart.getModelId());
        long count=orderCartMapper.countByExample(orderCartExample);
        if(count>0){
            return 3;
        }
        return orderCartMapper.insert(orderCart);
    }

    @Override
    public int delectOrder(OrderCartExample orderCart) {

        return orderCartMapper.deleteByExample(orderCart);
    }

    @Override
    public List<OrderCart> selectByShopId(OrderCartExample orderCartExample) {

        return orderCartMapper.selectByShopId(orderCartExample);
    }

    @Override
    public int updateCart(OrderCart orderCartExample) {
        return orderCartMapper.updateByPrimaryKeySelective(orderCartExample);
    }
    public OrderUserAddress insertOrderUserAddress(OrderUserAddress orderUserAddress){
        orderUserAddressMapper.insert(orderUserAddress);
        return  orderUserAddress;
    }
    public List<OrderUserAddress>selectExample(OrderUserAddressExample orderUserAddressExample){
        return orderUserAddressMapper.selectByExample(orderUserAddressExample);
    }
    public int updateAddress(OrderUserAddress orderUserAddress){
        return  orderUserAddressMapper.updateByPrimaryKey(orderUserAddress);
    }
    public OrderUserAddress getUserAddress(int id){
        return  orderUserAddressMapper.selectByPrimaryKey(id);
    }
    public int delectAddress(OrderUserAddressExample example){
        return orderUserAddressMapper.deleteByExample(example);
    }
    @Override
    public OrderUser inserllOrder(OrderUser orderUser) {
        orderUserMapper.insert(orderUser);
        return orderUser;
    }

    @Override
    public OrderUser selectOrder(OrderUserExample orderUserExample) {
        List<OrderUser> list=orderUserMapper.selectByExample(orderUserExample);
        return  list.get(0);

    }

    @Override
    public int inserllOrderInfo(OrderUserInfo orderUserInfo) {
        return  orderUserInfoMapper.insert(orderUserInfo);

    }

    @Override
    public int updateOrder(OrderUser orderUser) {
        return orderUserMapper.updateByPrimaryKey(orderUser);
    }

    @Override
    public int updateOrderInfo(OrderUserInfo orderUser) {
        return  orderUserInfoMapper.updateByPrimaryKey(orderUser);
    }

    @Override
    public List<OrderUser> selectOrderList(OrderUserExample example) {

        return orderUserMapper.selectByExample(example);
    }

    @Override
    public List<OrderUserInfo> seleclUserOrderInfoList(Integer example) {
        return orderUserInfoMapper.selectByExampleBygoods(example);
    }

    @Override
    public List<OrderUserInfo> selectOrderInfoList(OrderUserInfoExample example) {
        return orderUserInfoMapper.selectByExample(example);
    }

    @Override
    public int updataOrderNotity(OrderNotity orderNotity) {
        return orderNotityMapper.updateByPrimaryKey(orderNotity);
    }
    @Override
    public int installOrderNotity(OrderNotity orderNotity) {
        return orderNotityMapper.insert(orderNotity);
    }
    @Override
    public OrderNotity selectOrderNotyti(OrderNotityExample example) {
        List<OrderNotity> list = orderNotityMapper.selectByExample(example);
        return list.get(0);
    }

    @Override
    public List<OrderNotity> selectAllOrderNotity(OrderNotityExample example) {
        return  orderNotityMapper.selectByExample(example);
    }
    @Override
    public List<OrderUserInfo> selectByUserBygoods(Integer userId){
        return orderUserInfoMapper.selectByUserBygoods(userId);
    }
    @Override
    public List<OrderUserInfo> selectByUserByOrderUser(Integer orderUserId){
        return orderUserInfoMapper.selectByUserByOrderUser(orderUserId);
    }

    @Override
    public int installOrderUserDemand(OrderUserDemand data) {
        OrderUserDemandExample orderUserDemandExample  = new OrderUserDemandExample();
        orderUserDemandExample.createCriteria().andDemandIdEqualTo(data.getDemandId());
        List<OrderUserDemand> list = orderUserDemandMapper.selectByExample(orderUserDemandExample);
        if(null!=list&&0!=list.size()){
            return 0;

        }
        return orderUserDemandMapper.insert(data);
    }

    @Override
    public List<OrderUserDemand> selectOrderUserDemand(OrderUserDemandExample data) {
        return orderUserDemandMapper.selectByExample(data);
    }

    @Override
    public OrderUserDemand getOrderUserDemand(OrderUserDemandExample data) {
        List<OrderUserDemand> list = orderUserDemandMapper.selectByExample(data);
        if(null!=list&&0!=list.size()){
            return list.get(0);
        }
        return null;
    }

    @Override
    public int updateOrderUserDemand(OrderUserDemand data) {
        return orderUserDemandMapper.updateByPrimaryKey(data);
    }

    @Override
    public int delectOrderUserDemand(OrderUserDemand data) {
        return orderUserDemandMapper.deleteByPrimaryKey(data.getOrderId());
    }
}
